/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.karelun.whiteboard.gef.ui.actions;

import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.ActionFactory;
import org.karelun.whiteboard.gef.EditPart;
import org.karelun.whiteboard.gef.RequestConstants;
import org.karelun.whiteboard.gef.internal.GEFMessages;
import org.karelun.whiteboard.gef.requests.GroupRequest;

/**
 * An action to delete selected objects.
 */
public class DeleteAction extends SelectionAction {
	
	public DeleteAction(IEditorPart editor) {
		super(editor);
		setId(ActionFactory.DELETE.getId());
	}
	
	@Override
	protected Command createCommand() {
		List<?> objects = getSelectedEditParts();
		if (objects.isEmpty())
			return super.createCommand();
		if (!(objects.get(0) instanceof EditPart))
			return super.createCommand();
		return createDeleteCommand(objects);
	}

	public Command createDeleteCommand(List<?> objects) {
		GroupRequest deleteReq = new GroupRequest(RequestConstants.REQ_DELETE);
		deleteReq.setEditParts(objects);

		CompoundCommand compoundCmd = new CompoundCommand(
				GEFMessages.DeleteAction_ActionDeleteCommandName);
		for (int i = 0; i < objects.size(); i++) {
			EditPart object = (EditPart) objects.get(i);
			Command cmd = object.getCommand(deleteReq);
			if (cmd != null)
				compoundCmd.append(cmd);
		}

		return compoundCmd;
	}

//	protected void init() {
//		setText(GEFMessages.DeleteAction_Label);
//		setToolTipText(GEFMessages.DeleteAction_Tooltip);
//		setId(ActionFactory.DELETE.getId());
//		ISharedImages sharedImages = PlatformUI.getWorkbench()
//				.getSharedImages();
//		setImageDescriptor(sharedImages
//				.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
//		setDisabledImageDescriptor(sharedImages
//				.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
//		setEnabled(false);
//	}
 }
